package com.lwj.liwanjia.view.activity.myindent.tuikuantuihuo;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Rect;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.AllAlbumAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 所有图片
 */
public class AllAlbumActivity extends BaseActivity {

    @BindView(R.id.grid_album)
    SuperRecyclerView gridAlbum;
    @BindView(R.id.no_image)
    TextView no_iamge;
    private List<String> imageUrl = new ArrayList<>();
    private AllAlbumAdapter albumAdapter;

    @Override
    protected void initView() {
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        gridAlbum.setLayoutManager(manager);
        gridAlbum.setRefreshEnabled(false);
        gridAlbum.setLoadMoreEnabled(false);
        final String[] columns = {MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DATA};
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        Cursor imagecursor = this
                .getApplicationContext()
                .getContentResolver()
                .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                        null, null, orderBy + " DESC");
        for (int i = 0; i < imagecursor.getCount(); i++) {
            imagecursor.moveToPosition(i);
            int dataColumnIndex = imagecursor
                    .getColumnIndex(MediaStore.Images.Media.DATA);
            imageUrl.add(imagecursor.getString(dataColumnIndex));
        }
        gridAlbum.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                //不是第一个的格子都设一个左边和底部的间距
                outRect.left = (int) getResources().getDimension(R.dimen.x5);
                outRect.bottom = (int) getResources().getDimension(R.dimen.x5);
                //由于每行都只有4个，所以第一个都是4的倍数，把左边距设为0
                if (parent.getChildLayoutPosition(view) % 4 == 0) {
                    outRect.left = 0;
                }
            }
        });
        if (imageUrl.size() > 0) {
            no_iamge.setVisibility(View.GONE);
            gridAlbum.setVisibility(View.VISIBLE);
            albumAdapter = new AllAlbumAdapter(this, imageUrl);
            gridAlbum.setAdapter(albumAdapter);
        } else {
            no_iamge.setVisibility(View.VISIBLE);
            gridAlbum.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_all_album;
    }

    @OnClick({R.id.release, R.id.select})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.release://取消
                List<String> selectList1 = albumAdapter.getSelectList();
                if (selectList1 != null && selectList1.size() > 0) {
                    selectList1.clear();
                }
                finish();
                break;
            case R.id.select://确定
                List<String> selectList = albumAdapter.getSelectList();
                if (selectList.size() > 0) {
                    Intent intent = new Intent();
                    intent.putStringArrayListExtra("imageUrl", (ArrayList<String>) selectList);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    Toast.makeText(this, "你还没有选择照片", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
